<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="stylesheet" type="text/css" href="${ctx}/static/plug/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/static/plug/Hui-iconfont/1.0.8/iconfont.min.css" />
<style type="text/css">
.row {
	margin-top: 15px
}
</style>
<title>用户信息</title>
</head>
<body>
	<div class="panel panel-default">
		<div class="panel-body">
			<form id="dataForm">
				<div class="row cl">
					<input type="hidden" value="${bmsUser.id}" id="id" name="id"></input>
					<label class="form-label col-xs-2"><span class="c-red">*</span>用户账号：</label>
					<div class="form-label col-xs-4">
						<input class="input-text required" type="text" value="${bmsUser.entNo}" id="entNo" name="entNo"></input>
					</div>
				</div>
				<div class="row cl">
					<label class="form-label col-xs-2"><span class="c-red">*</span>用户类型：</label>
					<div class="form-label col-xs-4">
					<c:if test="${bmsRoleList != null && fn:length(bmsRoleList) > 0}">
						<select class="select required" id="type" name="type">
							<option value="">请选择用户类型</option>
							<c:forEach  items="${bmsRoleList}" var="bmsRole">
							<option value="${bmsRole.id}">${bmsRole.name}</option>
							</c:forEach>
						</select>
					</c:if>
					
					<c:if test="${bmsRoleList == null || fn:length(bmsRoleList) == 0}">
						<select class="select required" id="type" name="type">
							<option value="">暂无可用角色</option>
						</select>
						</c:if>
						
						
					</div>
					<c:if test="${scenicSpotId == null}">
						<label class="form-label col-xs-2">用户所属景区：</label>
						<div class="form-label col-xs-4">
							<select name="scenicSpotId" class="select" id="scenicSpotId">
								<option value="">请选择景区</option>
								<c:forEach var="scenicSpot" items="${scenicSpotList}">
									<c:if test="${bmsUser.scenicSpotId == scenicSpot.key}">
										<option value="${scenicSpot.key }" selected="selected">${scenicSpot.value}</option>
									</c:if>
									<c:if test="${!(bmsUser.scenicSpotId == scenicSpot.key)}">
										<option value="${scenicSpot.key }">${scenicSpot.value}</option>
									</c:if>
								</c:forEach>
							</select>
						</div>
					</c:if>
					<c:if test="${scenicSpotId != null}">
						<input type="hidden" id="scenicSpotId" name="scenicSpotId" value="${scenicSpotId}"></input>
					</c:if>
				</div>
				<div class="row cl" style="text-align: center">
					<button id="updateBtn" class="btn btn-secondary radius size-L" type="button">确定</button>
					<button id="cancelBtn" class="btn btn-secondary radius size-L" type="button">取消</button>
				</div>
			</form>
		</div>
	</div>
	<script type="text/javascript">
		var type = '${bmsUser.type}';
		if(type != null && type !='') {
			$("#type > option[value=" + type + "]").attr("selected","selected");
		}
	
		var canSubmit = true;
		$(function() {
			$("#updateBtn").click(function() {
				console.log($("#dataForm").length);
				if(!$("#dataForm").valid()) {
					return;
				}
				if(canSubmit) {
					canSubmit = false;
					var updateData = new Object();
					updateData.entNo = $("#entNo").val();
					updateData.type = $("#type").val();
					updateData.scenicSpotId = $("#scenicSpotId").val();
					updateData.id = $("#id").val();
					
					if(!updateData.type) {
						layer.msg("请选择用户类型!");
						canSubmit = true;
						return;
					}
					
					if(updateData.type != 2 && !updateData.scenicSpotId) {
						layer.msg("请选择用户所属景区!");
						canSubmit = true;
						return;
					}
					
					if(updateData.type == 2) {
						updateData.scenicSpotId = null;
					}
					
					var tips = "";
					if(updateData.id) {
						tips = "更新";
					} else {
						tips = "新增";
					}
					$.post("${ctx}/user/saveOrUpdate", updateData, function(data) {
						canSubmit = true;
						if (data.result) {
							if(!updateData.id) {
								tips = "新增成功，初始密码为：abc123"; 
							} else {
								tips = "更新成功"; 
							}
							layer.confirm(tips, {
								btn : [ '确定' ]
							}, function() {
								var index = parent.layer.getFrameIndex(window.name);
								parent.reloadData();
								parent.layer.close(index);
							});
						} else {
							layer.confirm(tips + "失败!" + data.errorMsg, {
								btn : [ '确定' ]
							}, function() {
								var index = parent.layer.getFrameIndex(window.name);
								parent.layer.close(index);
							});
						}
					});
				}
			});

			$("#cancelBtn").click(function() {
				var index = parent.layer.getFrameIndex(window.name);
				parent.layer.close(index);
			});
			
		});
	</script>
</body>
</html>
